import wepy from 'wepy'
import {
  UDID
} from '@/utils/constant'
function getCurrentTime() {
  let keep = ''
  let date = new Date()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '' + m + '' + d + '' + h + '' + f + '' + s
  return keep // 20160614134947
}
function getCurrentTimeData() {
  let keep = ''
  let date = new Date()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
  return keep // 2016-06-14 13:49:47
}
//东八区时间转换
//年月日时分秒
function getCurrentAdsTime(timeAds,num) {
  /// let ttt='2020.12.07 15:30:37'
  let startTime = timeAds.replace(/-/g,'/')
  //获取时间转换为时间戳
  let time=new Date(startTime).getTime()
  //时间戳减去东八区时间返回的时间戳？
  let asdtime=time-(8 * 60 * 60 * 1000)

  let keep = ''
  //h获取返回的时间
  let date = new Date(asdtime + num * 60 * 60 * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
  return keep // 2016-06-14 13:49:47
}
//东八区时间转换
//年月日时分秒
function getCurrentAdsDian(timeAds,num) {
  // let ttt='2020.12.07 15:30:37'
  let startTime = timeAds.replace(/\./g,'/')
  //获取时间转换为时间戳
  let time=new Date(startTime).getTime()
  //时间戳减去东八区时间返回的时间戳？
  let asdtime=time-(8 * 60 * 60 * 1000)

  let keep = ''
  //h获取返回的时间
  let date = new Date(asdtime + num * 60 * 60 * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  keep = y + '.' + m + '.' + d + ' ' + h + ':' + f + ':' + s
  return keep // 2016.06.14 13:49:47
}
//东八区时间转换
//年月日时分秒
//pattern 上传的字符串格式
//timeAds 时间字符串
//num 时区
//patternRetup抛出的 格式
function getCurrentAdsTimeYue(pattern,timeAds,num,patternRetu) {
    let date = new Date()
    let yyyyLth=pattern.indexOf("yyyy")
    let yyyy = date.getFullYear()
    if(yyyyLth!=-1){
      yyyy=timeAds.substring(yyyyLth,yyyyLth+4)
    }
    let MMLth=pattern.indexOf("MM")
    let MM = date.getMonth() + 1
        MM = MM < 10 ? '0' + MM : MM
   if(MMLth!=-1){
      MM=timeAds.substring(MMLth,MMLth+2)
   }
   let dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
   let ddLth=pattern.indexOf("dd")
   if(ddLth!=-1){
      dd=timeAds.substring(ddLth,ddLth+2)
   }
   let hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
   let hhLth=pattern.indexOf("HH")
   if(hhLth!=-1){
      hh=timeAds.substring(hhLth,hhLth+2)
   }
    let mm = '00'
   let mmLth=pattern.indexOf("mm")
   if(mmLth!=-1){
      mm=timeAds.substring(mmLth,mmLth+2)
   }
   let ss = '00'
   let ssLth=pattern.indexOf("ss")
   if(ssLth!=-1){
      ss=timeAds.substring(ssLth,ssLth+2)
   }
   let timeNew = yyyy + '-' + MM + '-' + dd + ' ' + hh + ':' + mm + ':' + ss
    return getCurrentAdsTimeShi(pattern,timeNew,num,patternRetu)
}
//东八区时间转换
//格式2020-12-05 19:54:00
function getCurrentAdsTimeShi(pattern,timeNew,num,patternRetu) {
  // return false
  //获取时间转换为时间戳
  let startTime = timeNew.replace(/-/g,'/')
  let time=new Date(startTime).getTime()
  //时间戳减去东八区时间返回的时间戳？
  let asdtime=time-(8 * 60 * 60 * 1000)
  let keep = ''
  //h获取返回的时间
  let date = new Date(asdtime + num * 60 * 60 * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  let f = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  // keep = y + '年' + m + '月' + d + '日' + ' ' + h + ':' + f + ':' + s
  keep=patternRetu.replace('yyyy',y)
  keep=keep.replace('MM',m)
  keep=keep.replace('dd',d)
  keep=keep.replace('HH',h)
  keep=keep.replace('mm',f)
  keep=keep.replace('ss',s)
  return keep // 返回格式2016年06月14日 13:49:47
}

//时间返回
function objLength(input) {
  let type = toString(input)
  let length = 0
  if (type !== '[object Object]') {
    // throw '输入必须为对象{}！'
  } else {
    for (let key in input) {
      if (key !== 'number') {
        length++
      }
    }
  }
  return length
}
// 验证是否是手机号码
function vailPhone(number) {
  let flag = true
  let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(17[0]{1})|(15[0-3]{1})|(15[5-9]{1})|(18[0-9]{1}))+\d{8})$/
  if (number.length !== 11 || !myreg.test(number)) {
    flag = false
  }
  return flag
}
// 验证是否西班牙手机(6开头 9位数)
function ifSpanish(number) {
  let flag = true
  let myreg = /^([6|7|9]{1}(\d+){8})$/
  if (number.length !== 9 || !myreg.test(number)) {
    flag = false
  }
  return flag
}
// 浮点型除法
function div(a, b) {
  let c, d, e, f
  try {
    e = a.toString().split('.')[1].length
  } catch (g) { }
  try {
    f = b.toString().split('.')[1].length
  } catch (g) {}
  // [eslint] Return statement should not contain assignment. (no-return-assign)
  c = Number(a.toString().replace('.', ''))
  d = Number(b.toString().replace('.', ''))
  return mul(c / d, Math.pow(10, f - e))
}
// 浮点型加法函数
function accAdd(arg1, arg2) {
  let r1, r2, m
  try {
    r1 = arg1.toString().split('.')[1].length
  } catch (e) {
    r1 = 0
  }
  try {
    r2 = arg2.toString().split('.')[1].length
  } catch (e) {
    r2 = 0
  }
  m = Math.pow(10, Math.max(r1, r2))
  return ((arg1 * m + arg2 * m) / m).toFixed(2)
}
// 浮点型乘法
function mul(a, b) {
  let c = 0
  let d = a.toString()
  let e = b.toString()
  try {
    c += a.toString().split('.')[1].length
  } catch (f) { }
  try {
    c += b.toString().split('.')[1].length
  } catch (f) { }
  return Number(d.replace('.', '')) * Number(e.replace('.', '')) / Math.pow(10, c)
}

//  遍历对象属性和值
function displayProp(obj) {
  let names = ''
  for (let name in obj) {
    names += name + obj[name]
  }
  return names
}
//  去除字符串所有空格
function sTrim(text) {
  return text.replace(/\s/g, '')
}
// 去除所有:,英文冒号
function replaceColon(txt) {
  return txt.replace(/:/g, '')
}
// 转换星星分数
function convertStarArray(score) {
  // 1 全星,0 空星,2半星
  let arr = []
  for (let i = 1; i <= 5; i++) {
    if (score >= i) {
      arr.push(1)
    } else if (score > i - 1 && score < i + 1) {
      arr.push(2)
    } else {
      arr.push(0)
    }
  }
  return arr
}
//广告位跳转
function goToAdvertOne(type,content,goodsId,productId) {
      	if(type=="goods"){
          if(content.indexOf("-")!= -1){
            let arr3 = content.split("-");
            let productId=''
            let goodsId=''
            if(arr3!='' && arr3!=null && arr3!=undefined){
              productId=arr3[0]
              goodsId=arr3[1]
            }
             wepy.navigateTo({
                 url: "/pages/goods/goods?goodsId="+goodsId+"&id="+productId
            	})
          }else{
            wepy.navigateTo({
                url: "/pages/goods/goods?goodsId="+goodsId+"&id="+productId
            })
          }
      	}else if(type== "product"){
      		if(content.indexOf("-")!= -1){
      		  let arr3 = content.split("-");
      		  let productId=''
      		  let goodsId=''
      		  if(arr3!='' && arr3!=null && arr3!=undefined){
      		    productId=arr3[0]
      		    goodsId=arr3[1]
      		  }
      		   wepy.navigateTo({
      		       url: "/pages/goods/goods?goodsId="+goodsId+"&id="+productId
      		  	})
      		}else{
      		  wepy.navigateTo({
      		      url: "/pages/goods/goods?goodsId="+goodsId+"&id="+productId
      		  })
      		}
      	}else if(type== "brand"){
      		 wepy.navigateTo({
      		  url: "/pages/brand/brand_page?id="+content
      		})

      	}else if(type== "cate"){
    			 wepy.navigateTo({
    			  url: "/pages/brand/brand_cate?id="+content
    			})

    		}else if(type=="garss"){
      		 wepy.navigateTo({
      		  url: "/pages/discover/grass?id="+content
      		})
      	}else if(type == 'url'){
            wepy.navigateTo({
              url: "/pages/home/webview?url="+encodeURIComponent(content)
            })
        }else if(type == 'ticket'){
          wepy.navigateTo({
            url: "/packageA/pages/person/coupon/couponCenter"
          })
        }else if(type == 'group'){
          wepy.navigateTo({
            url: "/packageA/pages/person/group/group"
          })
        }else if(type == 'juhuasuan'){
          wepy.navigateTo({
            url: "/pages/home/kesalanPath"
          })
        }else if(type == 'willSell'){
          wepy.navigateTo({
            url: "/pages/home/welfare"
          })
        }else if(type == 'times'){
          wepy.navigateTo({
            url: "/pages/home/timeBuy"
          })
        }else if(type == 'label'){
          wepy.navigateTo({
            url: "/pages/home/biaoqian?id="+content
          })
        }else if(type == 'live'){
            if(productId!='' && productId!=null){
              wx.navigateTo({
                  url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id='+productId
              })
            }else{
              wepy.navigateTo({
                url: '/packageA/pages/person/live/liveInfo?id='+content+'&type='+type
              })
            }
        }else if(type == 'playBack'){
          if(productId!='' && productId!=null){
            wx.navigateTo({
              url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id='+productId
            })
          }else{
            wepy.navigateTo({
              url: '/packageA/pages/person/live/liveInfo?id='+content+'&type='+type
            })
          }
        }else if(type == 'pointGoods'){
        wepy.navigateTo({
          url: '/packageA/pages/person/points/points'
        })
        }else if(type == 'liveGoods'){
          wepy.navigateTo({
            url: '/packageA/pages/live/liveList'
          })
        }else if(type == 'fresh'){
    	  wepy.navigateTo({
    	    url: '/packageA/pages/person/fresh/freshIndex'
    	  })
      }else if(type == 'myTicket'){
    	  wepy.navigateTo({
    	    url: '/packageA/pages/person/coupon/coupon'
    	  })
      }
       }
module.exports = {
  getCurrentTime: getCurrentTime,
  getCurrentTimeData:getCurrentTimeData,
  objLength: objLength,
  displayProp: displayProp,
  sTrim: sTrim,
  replaceColon: replaceColon,
  vailPhone: vailPhone,
  ifSpanish: ifSpanish,
  div: div,
  mul: mul,
  accAdd: accAdd,
  convertStarArray: convertStarArray,
  getCurrentAdsTime:getCurrentAdsTime,
  getCurrentAdsTimeYue:getCurrentAdsTimeYue,
  getCurrentAdsTimeShi:getCurrentAdsTimeShi,
  getCurrentAdsDian:getCurrentAdsDian,
  goToAdvertOne:goToAdvertOne
}
